/*  Copyright (C) 2009 Jos van der Til
    
    This file is part of the Fluent Metadata Library.

    The Fluent Metadata Library is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    The Fluent Metadata Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with the Fluent Metadata Library.  If not, see <http://www.gnu.org/licenses/>.
 */
using System.ComponentModel.DataAnnotations;
using System.Linq;
using FluentMetadata.AttributeBuilders;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FluentMetadata.Tests.AttributeBuilders.Display
{
    [TestClass]
    public class DisplayAttributeBuilderTest
    {
        private DisplayAttribute GetAttribute(FluentMetadata metadata, string property)
        {
            return (metadata.AttributeBuilders[property].First() as AbstractAttributeBuilder).GetAttributes()
                                                                                             .First() as DisplayAttribute;
        }

        [TestMethod]
        public void Can_Set_Displayed_Name()
        {
            var attribute = GetAttribute(new DisplayTestEntityMetadata(), "Price");

            Assert.AreEqual("Price in catalog", attribute.Name);
        }

        [TestMethod]
        public void Can_Set_Displayed_Description()
        {
            var attribute = GetAttribute(new DisplayTestEntityMetadata(), "Price");

            Assert.AreEqual("The price in the catalog is stored here.", attribute.Description);
        }

        [TestMethod]
        public void Can_Set_AutoGenerateField_Property()
        {
            var attribute = GetAttribute(new DisplayTestEntityMetadata(), "Id");

            Assert.IsTrue(attribute.AutoGenerateField);
        }

        [TestMethod]
        public void Can_Set_AutoGenerateFilter_Property()
        {
            var attribute = GetAttribute(new DisplayTestEntityMetadata(), "Name");

            Assert.IsTrue(attribute.AutoGenerateFilter);
        }
    }

    class DisplayTestEntity
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public double Price { get; set; }
    }

    class DisplayTestEntityMetadata : FluentMetadata<DisplayTestEntity>
    {
        public DisplayTestEntityMetadata()
        {
            ForProperty(te => te.Price).SetDisplayProperty
                                       .DisplayedName("Price in catalog")
                                       .DisplayedDescription("The price in the catalog is stored here.");

            ForProperty(te => te.Name).SetDisplayProperty.AutoGenerateFilter(true);

            ForProperty(te => te.Id).SetDisplayProperty.AutoGenerateField(true);
        }
    }
}